//package com.cli.springboottemplateengine.engine.analyze;
//
//import org.codehaus.groovy.ast.*;
//import org.codehaus.groovy.ast.expr.*;
//import org.codehaus.groovy.ast.builder.AstBuilder;
//import org.codehaus.groovy.ast.stmt.BlockStatement;
//import org.codehaus.groovy.ast.stmt.ExpressionStatement;
//import org.codehaus.groovy.ast.stmt.Statement;
//import org.codehaus.groovy.control.CompilePhase;
//
//
//import java.util.*;
//import java.util.regex.*;
//
//public class GroovyVariableAnalyzer implements TemplateVariableAnalyzer {
//
//    @Override
//    public Set<String> extractVariables(String templateContent) {
//        Set<String> variables = new LinkedHashSet<>();
//
//        Pattern pattern = Pattern.compile("\\$\\{(.*?)}|\\$(\\w+)");
//        Matcher matcher = pattern.matcher(templateContent);
//
//        Set<String> expressions = new LinkedHashSet<>();
//        while (matcher.find()) {
//            if (matcher.group(1) != null) {
//                expressions.add(matcher.group(1));
//            } else if (matcher.group(2) != null) {
//                expressions.add(matcher.group(2));
//            }
//        }
//
//        for (String expr : expressions) {
//            try {
//                List<ASTNode> nodes = new AstBuilder().buildFromString(CompilePhase.CONVERSION, false, expr);
//                for (ASTNode node : nodes) {
//                    if (node instanceof Expression) {
//                        collectVariables((Expression) node, variables);
//                    }
//                }
//            } catch (Exception e) {
//                // 解析失败忽略
//            }
//        }
//
//        return variables;
//    }
//
//    private void collectVariables(Expression expr, Set<String> vars) {
//        if (expr == null) return;
//
//        if (expr instanceof VariableExpression) {
//            vars.add(((VariableExpression) expr).getName());
//        } else if (expr instanceof PropertyExpression) {
//            vars.add(buildFullPath((PropertyExpression) expr));
//        } else if (expr instanceof MethodCallExpression) {
//            collectVariables(((MethodCallExpression) expr).getObjectExpression(), vars);
//            for (Expression arg : ((MethodCallExpression) expr).getArguments().) {
//                collectVariables(arg, vars);
//            }
//        } else if (expr instanceof BinaryExpression) {
//            collectVariables(((BinaryExpression) expr).getLeftExpression(), vars);
//            collectVariables(((BinaryExpression) expr).getRightExpression(), vars);
//        } else if (expr instanceof ClosureExpression) {
//            collectVariables(((ClosureExpression) expr).getCode(), vars);
//        } else if (expr instanceof BlockStatement) {
//            for (Statement stmt : ((BlockStatement) expr).getStatements()) {
//                if (stmt instanceof ExpressionStatement) {
//                    collectVariables(((ExpressionStatement) stmt).getExpression(), vars);
//                }
//            }
//        } else if (expr instanceof ListExpression) {
//            for (Expression item : ((ListExpression) expr).getExpressions()) {
//                collectVariables(item, vars);
//            }
//        } else {
//            for (ASTNode child : expr.getChildren()) {
//                if (child instanceof Expression) {
//                    collectVariables((Expression) child, vars);
//                }
//            }
//        }
//    }
//
//    private String buildFullPath(PropertyExpression expr) {
//        List<String> parts = new ArrayList<>();
//        Expression current = expr;
//        while (current instanceof PropertyExpression) {
//            PropertyExpression pe = (PropertyExpression) current;
//            Expression prop = pe.getProperty();
//            if (prop instanceof ConstantExpression) {
//                parts.add(0, prop.getValue().toString());
//            } else if (prop instanceof VariableExpression) {
//                parts.add(0, ((VariableExpression) prop).getName());
//            }
//            current = pe.getObjectExpression();
//        }
//        if (current instanceof VariableExpression) {
//            parts.add(0, ((VariableExpression) current).getName());
//        }
//        return String.join(".", parts);
//    }
//}
